<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增</title>
    <link rel="stylesheet" href="./css/reset.css">
    <style>
        body {
            background-color: gainsboro;
        }

        #app {
            width: 300px;
            height: 400px;
            background-color: #fff;
            margin: 100px auto;
            padding: 10px 14px;
        }

        .top {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .text {
            width: 220px;
            height: 30px;
            padding: 0 10px;
            border: 1px solid gainsboro;
        }

        .add {
            width: 40px;
            height: 28px;
            color: #fff;
            letter-spacing: 1px;
            border-radius: 5px;
            background-color: rgb(23, 233, 23);
        }

        nav {
            display: flex;
            justify-content: space-between;
        }

        nav div {
            padding: 5px;
            color: #fff;
            background-color: rgb(23, 233, 23);
            border-radius: 5px;
            font-size: 14px;
        }

        .not {
            background-color: red;
        }

        p {
            font-weight: 700;
        }

        .list {
            height: 350px;
            padding: 10px 0;
            color: #fff;
            overflow-y: auto;
        }

        .item {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: red;
            margin-bottom: 10px;
        }

        .finished {
            background-color: rgb(23, 233, 23);
        }

        .delete,
        .name {
            cursor: pointer;
        }

        .empty {
            color: red;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="top">
            <input class="text" type="text" placeholder="请输入任务名称" v-model="inputText">
            <button class="add" v-on:click="add">新增</button>
        </div>
        <nav>
            <div>全部任务(<span v-text="all"></span>)</div>
            <div>已完成(<span v-text="completed"></span>)</div>
            <div class="not">未完成(<span v-text="not"></span>)</div>
        </nav>
        <p>任务列表</p>
        <div class="list">
            <div v-bind:class="`item ${item.status? 'finished' : ''}`" v-for="(item,index) in tasklist"
                v-bind:key="index">
                <input type="checkbox" v-on:click="changeStatus(item)">
                <div class="name">{{item.name}}</div>
                <div class="delete" v-on:click="remove(item)">删除</div>
            </div>
            <div class="empty" v-show="tasklist.length == 0">任务列表为空，请添加一个任务！</div>
        </div>

    </div>
</body>

</html>
<script src="./js/vue-2.5.21.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: {
            inputText: '',
            tasklist: [],
            all: '0',
            completed: '0',
            not: '0'
        },
        methods: {
            add() {
                if (this.inputText) {
                    this.tasklist.push({ id: this.tasklist.length + 1, name: this.inputText, status: false })
                    this.inputText = ''
                    this.all = this.tasklist.length
                    this.not = this.tasklist.length
                }
            },
            changeStatus(item) {
                item.status = !item.status
                if (item.status) {
                    this.completed++
                    if (this.not > 0) {
                        this.not--
                    }
                } else {
                    if (this.completed > 0) {
                        this.completed--
                    }
                    this.not++
                }
            },
            remove(item) {
                let i = this.tasklist.indexOf(item)
                this.tasklist.splice(i, 1)
            }
        }
    })
</script>